﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Collections;

namespace Manager
{
    public class AssetBundleLoader
    {
        public string url;

        public AssetBundle bundle;

        public Action<AssetBundleLoader> completeCallBack;

        public Action<AssetBundleLoader> errorCallBack;

        public AssetBundleLoader(string _url, Action<AssetBundleLoader> _completeCallBack, Action<AssetBundleLoader> _errorCallBack)
        {
            url = _url;
            completeCallBack = _completeCallBack;
            errorCallBack = _errorCallBack;
            GameManager.instance.StartCoroutine(StartLoad());
        }

        IEnumerator StartLoad()
        {
            string path = AppConst.GetDataPath(url);
            AssetBundleCreateRequest result = AssetBundle.LoadFromFileAsync(path);

            //Debug.LogError("准备加载ab:" + url);
            //WWW www = new WWW(path);
            yield return result;
            if (result.isDone)
            {
                //Debug.LogError("ab加载成功:" + url);
                bundle = result.assetBundle;
                if (completeCallBack != null)
                    completeCallBack(this);
            }
            else
            {
                if (errorCallBack != null)
                    errorCallBack(this);
            }
          
        }

    }

}
